package com.ruoyi.channel.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ChannelExtInfoMapper;
import com.ruoyi.db.domain.ChannelExtInfo;
import com.ruoyi.channel.service.IChannelExtInfoService;

import javax.annotation.Resource;

/**
 * 渠道扩展信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-13
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ChannelExtInfoServiceImpl implements IChannelExtInfoService 
{
    @Resource
    private ChannelExtInfoMapper channelExtInfoMapper;

    /**
     * 查询渠道扩展信息
     * 
     * @param channelCode 渠道扩展信息主键
     * @return 渠道扩展信息
     */
    @Override
    public ChannelExtInfo selectChannelExtInfoByChannelCode(String channelCode)
    {
        ChannelExtInfo channelExtInfo = channelExtInfoMapper.selectChannelExtInfoByChannelCode(channelCode);
        if (ObjectUtil.isEmpty(channelExtInfo)){
            channelExtInfo = new ChannelExtInfo();
            channelExtInfo.setChannelCode(channelCode);
            channelExtInfo.setCreateTime(new Date());
            channelExtInfoMapper.insertChannelExtInfo(channelExtInfo);

            channelExtInfo = channelExtInfoMapper.selectChannelExtInfoByChannelCode(channelCode);
        }
        return channelExtInfo;
    }

    /**
     * 查询渠道扩展信息列表
     * 
     * @param channelExtInfo 渠道扩展信息
     * @return 渠道扩展信息
     */
    @Override
    public List<ChannelExtInfo> selectChannelExtInfoList(ChannelExtInfo channelExtInfo)
    {
        return channelExtInfoMapper.selectChannelExtInfoList(channelExtInfo);
    }

    /**
     * 查询渠道扩展信息列表
     *
     * @param channelExtInfo 渠道扩展信息
     * @return 渠道扩展信息
     */
    @Override
    public List<Map<String, Object>> selectChannelExtInfoMapList(ChannelExtInfo channelExtInfo)
    {
        return channelExtInfoMapper.selectChannelExtInfoMapList(channelExtInfo);
    }

    /**
     * 新增渠道扩展信息
     * 
     * @param channelExtInfo 渠道扩展信息
     * @return 结果
     */
    @Override
    public int insertChannelExtInfo(ChannelExtInfo channelExtInfo)
    {
        channelExtInfo.setCreateTime(DateUtils.getNowDate());
        return channelExtInfoMapper.insertChannelExtInfo(channelExtInfo);
    }

    /**
     * 修改渠道扩展信息
     * 
     * @param channelExtInfo 渠道扩展信息
     * @return 结果
     */
    @Override
    public int updateChannelExtInfo(ChannelExtInfo channelExtInfo)
    {
        if (ObjectUtil.isNotEmpty(channelExtInfo.getZhimaFilterArray())){
            channelExtInfo.setZhimaFilter(JSON.toJSONString(channelExtInfo.getZhimaFilterArray()));
        }
        channelExtInfo.setUpdateTime(DateUtils.getNowDate());
        return channelExtInfoMapper.updateChannelExtInfo(channelExtInfo);
    }

    /**
     * 批量删除渠道扩展信息
     * 
     * @param channelCodes 需要删除的渠道扩展信息主键
     * @return 结果
     */
    @Override
    public int deleteChannelExtInfoByChannelCodes(String[] channelCodes)
    {
        return channelExtInfoMapper.deleteChannelExtInfoByChannelCodes(channelCodes);
    }

    /**
     * 删除渠道扩展信息信息
     * 
     * @param channelCode 渠道扩展信息主键
     * @return 结果
     */
    @Override
    public int deleteChannelExtInfoByChannelCode(String channelCode)
    {
        return channelExtInfoMapper.deleteChannelExtInfoByChannelCode(channelCode);
    }
}
